import { createApp } from 'vue'
import './style.css'
import App from './App.vue'

// 引入路由
import router from './routers/index'

// 引入UI组件
import Vant from 'vant';
// 引入组件样式
import 'vant/lib/index.css';

//引入cookie插件
import VueCookies from "vue3-cookies";

// 引入API
import Api from './api/index';
import BaiduMap from 'vue-baidu-map-3x'
import QrcodeVue from 'qrcode-vue3';

var app = createApp(App)
    .use(router)
    .use(BaiduMap, {
        // ak 是在百度地图开发者平台申请的密钥 详见 http://lbsyun.baidu.com/apiconsole/key */
        ak: 'hpgQp5hLRT2j9ZBTc7sC8LQzTQGOyP9k',
        // v:'2.0',  // 默认使用3.0
        // type: 'WebGL' // ||API 默认API  (使用此模式 BMap=BMapGL)
      })
    .use(Vant)
    .use(QrcodeVue)
    .use(VueCookies)
    .use(Api)
    .mount('#app')


router.beforeEach(async (to, from, next) => {
    
    let isLogin = to.meta.isLogin ?? false;

    if (isLogin) {

        let LoginUser = app.$cookies.get('LoginUser') ?? {}

        if (JSON.stringify(LoginUser) == '{}') {
            error('未登陆，请先去登陆')
            next('/business/login');
            return false;
        }

        let data = {
            id: LoginUser.id ?? 0,
            mobile: LoginUser.mobile ?? ''
        }
        let result = await app.$api.check(data);

        if (result.code == 1) {
            app.$cookies.set("LoginUser", result.data);
            next();
        } else {
            error('非法登陆')
            next('/business/login');
            return false;
        }
    } else {
        next()
    }
})